<?php

namespace App\Http\Controllers\Admin;

use App\Member;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class LoginController extends Controller
{
    public function index(Request $request)
    {
        return view('admin.login');
    }

    public function doLogin(Request $request)
    {
        $member = Member::where('name', $request->input('name'))->first();
        if (!isset($member->id)) {
            $return = ['status' => 'n', 'info' => '不存在此管理员！'];
            return response()->json($return);
        }
        if (strcmp(decrypt($member->pwd), $request->input('pwd')) != 0) {
            $return = ['status' => 'n', 'info' => '管理员密码不正确！'];
            return response()->json($return);
        }
        $loginAt = date('Y-m-d H:i:s');
        $ip = $request->server('REMOTE_ADDR');
        $request->session()->put('adminId', $member->id);
        $request->session()->put('adminName', $member->name);
        $request->session()->put('loginAt', $member->login_at);
        $request->session()->put('ip', $member->ip);
        $member->login_at = $loginAt;
        $member->ip = $ip;
        if ($member->save()) {
            $return = ['status' => 'y'];
            return response()->json($return);
        } else {
            $return = ['status' => 'n', 'info' => '请稍候重试！'];
            return response()->json($return);
        }
    }
}